/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.message;

import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.response.PaginationEntity;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.service.MessageLookupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/message")
@Api(produces = "application/json", tags = "历史消息查询接口" )
@Validated
public class LookupController {

	@Resource
	private MessageLookupService messageLookupService;

	@ApiOperation(httpMethod = "GET", value = "获取历史群消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "群组ID", paramType = "query",  dataTypeClass = Long.class,example = "0",required = true),
			@ApiImplicitParam(name = "fromId", value = "当前消息ID(最小ID)", paramType = "query", dataTypeClass = Long.class),
	})
	@GetMapping(value = "/lookup/group")
	public PaginationEntity<Message> findGroupList(@RequestParam long id, @RequestParam(required = false) Long fromId) {

		Page<Message> page = messageLookupService.findGroup(id,fromId == null ? 0 : fromId);

		return PaginationEntity.make(page);
	}


	@ApiOperation(httpMethod = "GET", value = "获取历史公众号消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "公众号ID", paramType = "query",  dataTypeClass = Long.class,example = "0",required = true),
			@ApiImplicitParam(name = "fromId", value = "当前消息ID(最小ID)", paramType = "query", dataTypeClass = Long.class),
	})
	@GetMapping(value = "/lookup/app")
	public PaginationEntity<Message> findAppList(@RequestParam long id, @RequestParam(required = false) Long fromId) {

		Page<Message> page = messageLookupService.findApp(id,fromId == null ? 0 : fromId);

		return PaginationEntity.make(page);
	}

	@ApiOperation(httpMethod = "GET", value = "获取历史单聊消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "好友UID", paramType = "query",  dataTypeClass = Long.class,example = "0",required = true),
			@ApiImplicitParam(name = "fromId", value = "当前消息ID(最小ID)", paramType = "query", dataTypeClass = Long.class),
	})
	@GetMapping(value = "/lookup/p2p")
	public PaginationEntity<Message> findP2pList(@ApiIgnore @UID Long uid, @RequestParam long id, @RequestParam(required = false) Long fromId) {

		Page<Message> page = messageLookupService.findP2p(uid,id,fromId == null ? 0 : fromId);

		return PaginationEntity.make(page);
	}

	@ApiOperation(httpMethod = "GET", value = "获取历史系统消息")
	@ApiImplicitParam(name = "fromId", value = "当前消息ID(最小ID)", paramType = "query", dataTypeClass = Long.class)
	@GetMapping(value = "/lookup/system")
	public PaginationEntity<Message> findSystemList(@RequestParam(required = false) Long fromId) {

		Page<Message> page = messageLookupService.findSystem(fromId == null ? 0 : fromId);

		return PaginationEntity.make(page);
	}


	@ApiOperation(httpMethod = "GET", value = "获取离线消息")
	@GetMapping(value = {"/lookup/offline","/list/offline"})
	public ResponseEntity<List<Message>> offlineList(@ApiIgnore @UID Long uid) {

		List<Message> messageList = messageLookupService.findOfflineList(uid);

		messageList.forEach(message -> message.setReceiver(uid));

		return ResponseEntity.ok(messageList);
	}
}
